package com.elvis.chat.ui;

import com.elvis.chat.R;

import android.content.Context;

public class XMPPErrorType {
    /**
     * Represents a XMPP error sub-packet. Typically, a server responds to a request that has problems by sending the
     * packet back and including an error packet. Each error has a code, type, error condition as well as as an optional
     * text explanation. Typical errors are:
     * <p>
     * <table border=1>
     * <hr>
     * <td><b>Code</b></td>
     * <td><b>XMPP Error</b></td>
     * <td><b>Type</b></td>
     * </hr>
     * <tr>
     * <td>500</td>
     * <td>interna-server-error</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>403</td>
     * <td>forbidden</td>
     * <td>AUTH</td>
     * </tr>
     * <tr>
     * <td>400</td
     * <td>bad-request</td>
     * <td>MODIFY</td>>
     * </tr>
     * <tr>
     * <td>404</td>
     * <td>item-not-found</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>409</td>
     * <td>conflict</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>501</td>
     * <td>feature-not-implemented</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>302</td>
     * <td>gone</td>
     * <td>MODIFY</td>
     * </tr>
     * <tr>
     * <td>400</td>
     * <td>jid-malformed</td>
     * <td>MODIFY</td>
     * </tr>
     * <tr>
     * <td>406</td>
     * <td>no-acceptable</td>
     * <td>MODIFY</td>
     * </tr>
     * <tr>
     * <td>405</td>
     * <td>not-allowed</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>401</td>
     * <td>not-authorized</td>
     * <td>AUTH</td>
     * </tr>
     * <tr>
     * <td>402</td>
     * <td>payment-required</td>
     * <td>AUTH</td>
     * </tr>
     * <tr>
     * <td>404</td>
     * <td>recipient-unavailable</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>302</td>
     * <td>redirect</td>
     * <td>MODIFY</td>
     * </tr>
     * <tr>
     * <td>407</td>
     * <td>registration-required</td>
     * <td>AUTH</td>
     * </tr>
     * <tr>
     * <td>404</td>
     * <td>remote-server-not-found</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>504</td>
     * <td>remote-server-timeout</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>502</td>
     * <td>remote-server-error</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>500</td>
     * <td>resource-constraint</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>503</td>
     * <td>service-unavailable</td>
     * <td>CANCEL</td>
     * </tr>
     * <tr>
     * <td>407</td>
     * <td>subscription-required</td>
     * <td>AUTH</td>
     * </tr>
     * <tr>
     * <td>500</td>
     * <td>undefined-condition</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>400</td>
     * <td>unexpected-condition</td>
     * <td>WAIT</td>
     * </tr>
     * <tr>
     * <td>408</td>
     * <td>request-timeout</td>
     * <td>CANCEL</td>
     * </tr>
     * </table>
     * @author Matt Tucker
     */
    /*
     * <bad-request/> -- 发送者发送的XML是不规范的或不能被处理(例如 一个IQ节包含了一个未被承认的'type'属性值); 相关的错误类型应该（SHOULD）是"modify". <conflict/> --
     * 不同意访问，因为相同的名字或地址已存在一个资源或会话;相关的错误类型应该（ SHOULD）是"cancel". <feature-not-implemented/> --
     * 请求的特性未被接收者或服务器实现所以不能处理;相关的错误类型应该（SHOULD）是"cancel". <forbidden/> -- 请求实体没有必需的许可来执行这一动作;相关的错误类型应该（SHOULD）是"auth".
     * <gone/> -- 接收者或服务器无法再以这个地址进行联系(错误节可以（MAY）在<gone/>元素的XML字符数据中包含一个新的地址);相关的错误类型应该（SHOULD）是"modify".
     * <internal-server-error/> -- 服务器不能处理节，因为错误的配置或其他未定义的内部服务器错误;相关的错误类型应该（SHOULD）是"wait". <item-not-found/> --
     * JID地址或申请的条目无法找到;相关的错误类型应该（SHOULD）是"cancel". <jid-malformed/> --
     * 发送的实体提供的XMPP地址或与之通信的某个XMPP地址(如一个'to'属性值)或这个XMPP地址中的一部分(如一个资源ID)不符合寻址方案的语法(第三章);相关的错误类型应该（SHOULD）是"modify".
     * <not-acceptable/> -- 接收者或服务器理解这个请求但是拒绝处理，因为它不符合某些接收者或服务器定义的标准(例如,一个关于消息中可接收的单词的本地策略);相关错误类型应该（SHOULD）是"modify".
     * <not-allowed/> -- 接收者或服务器不允许任何实体执行这个动作;相关错误类型应该（SHOULD）是"cancel". <not-authorized/> --
     * 在被允许执行某个动作之前发送者必须提供适当的证书,或已提供了不正确的证书;相关错误类型应该（SHOULD）是"auth". <payment-required/> --
     * 请求实体未被授权访问请求的服务，因为需要付费;相关错误类型应该（SHOULD）是"auth". <recipient-unavailable/> --
     * 预定的接收者暂时不可用;相关错误类型应该（SHOULD）是"wait"(注意: 如果这样做会导致泄露预定接收者的网络可用性给一个未被授权了解此信息的实体，应用程序不应该（MUST NOT）返回这个错误).
     * <redirect/> -- 接收者或服务器重定向这个请求信息到另一个实体，通常是暂时的(这个错误节应该
     * （SHOULD）在<redirect/>元素的XML字符数据中包含一个预备的地址,它必须（MUST）是一个合法的JID); 相关的错误类型应该（SHOULD）是"modify".
     * <registration-required/> -- 请求实体未被授权访问请求的服务，因为需要注册;相关错误类型应该（SHOULD）是"auth". <remote-server-not-found/> --
     * 在预定的接收者的全部或部分JID中的一个远程服务器或服务不存在;相关错误类型应该（SHOULD）是"cancel". <remote-server-timeout/> --
     * 在预定的接收者（或需要完成的一个申请）的全部或部分JID中的一个远程服务器或服务无法在合理的时间内联系到;相关错误类型应该（SHOULD）是"wait". <resource-constraint/> --
     * 服务器或接收者缺乏足够的系统资源来服务请求;相关错误类型应该（SHOULD）是"wait". <service-unavailable/> --
     * 服务器或接收者目前无法提供被请求的服务;相关错误类型应该（SHOULD）是"cancel". <subscription-required/> --
     * 请求实体未被授权访问能请求的服务，因为需要订阅;相关错误类型应该（SHOULD）是"auth". <undefined-condition/> --
     * 错误条件不是本列表中定于的其他条件之一;任何错误类型可能和这个条件有关,并且它应该（SHOULD）仅用于关联一个应用程序定义的条件. <unexpected-request/> --
     * 接收者或服务器理解这个请求但是不希望是在这个时间(比如,请求的顺序颠倒);相关错误类型应该（SHOULD）是"wait".
     */

    public static final int NoError = 0;

    public static final int ERROR_INTERNA_SERVER_ERROR = 1;
    public static final int ERROR_FORBIDDEN = 2;
    public static final int ERROR_BAD_REQUEST = 3;
    public static final int ERROR_CONFLICT = 4;
    public static final int ERROR_FEATURE_NOT_IMPLEMENTED = 5;
    public static final int ERROR_GONE = 6;
    public static final int ERROR_ITEM_NOT_FOUND = 7;
    public static final int ERROR_JID_MALFORMED = 8;
    public static final int ERROR_NO_ACCEPTABLE = 9;
    public static final int ERROR_NOT_ALLOWED = 10;
    public static final int ERROR_NOT_AUTHORIZED = 11;
    public static final int ERROR_PAYMENT_REQUIRED = 12;
    public static final int ERROR_RECIPIENT_UNAVAILABLE = 13;
    public static final int ERROR_REDIRECT = 14;
    public static final int ERROR_REGISTRATION_REQUIRED = 15;
    public static final int ERROR_REMOTE_SERVER_ERROR = 16;
    public static final int ERROR_REMOTE_SERVER_NOT_FOUND = 17;
    public static final int ERROR_REMOTE_SERVER_TIMEOUT = 18;
    public static final int ERROR_RESOURCE_CONSTRAINT = 19;
    public static final int ERROR_SERVICE_UNAVAILABLE = 20;
    public static final int ERROR_SUBSCRIPTION_REQUIRED = 21;
    public static final int ERROR_UNDEFINED_STRING = 22;
    public static final int ERROR_UNEXPECTED_REQUEST = 23;
    public static final int ERROR_REQUEST_TIMEOUT = 24;
    public static final int ERROR_UNKNOWN = 25;

    public static final String interna_server_error = "internal-server-error";
    public static final String forbidden = "forbidden";
    public static final String bad_request = "bad-request";
    public static final String conflict = "conflict";
    public static final String feature_not_implemented = "feature-not-implemented";
    public static final String gone = "gone";
    public static final String item_not_found = "item-not-found";
    public static final String jid_malformed = "jid-malformed";
    public static final String no_acceptable = "not-acceptable";
    public static final String not_allowed = "not-allowed";
    public static final String not_authorized = "not-authorized";
    public static final String payment_required = "payment-required";
    public static final String recipient_unavailable = "recipient-unavailable";
    public static final String redirect = "redirect";
    public static final String registration_required = "registration-required";
    public static final String remote_server_error = "remote-server-error";
    public static final String remote_server_not_found = "remote-server-not-found";
    public static final String remote_server_timeout = "remote-server-timeout";
    public static final String resource_constraint = "resource-constraint";
    public static final String service_unavailable = "service-unavailable";
    public static final String subscription_required = "subscription-required";
    public static final String undefined_String = "undefined-String";
    public static final String unexpected_request = "unexpected-request";
    public static final String request_timeout = "request-timeout";

    /**
     * Get the presence type from a presence packet.
     * @param presence the presence type
     * @return an int representing the presence type
     */
    public static int getErrorType(final String errorCode) {
	int res = NoError;
	if (errorCode.equals(interna_server_error)) {
	    res = ERROR_INTERNA_SERVER_ERROR;
	} else if (errorCode.equals(forbidden)) {
	    res = ERROR_FORBIDDEN;
	} else if (errorCode.equals(bad_request)) {
	    res = ERROR_BAD_REQUEST;
	} else if (errorCode.equals(conflict)) {
	    res = ERROR_CONFLICT;
	} else if (errorCode.equals(feature_not_implemented)) {
	    res = ERROR_FEATURE_NOT_IMPLEMENTED;
	} else if (errorCode.equals(gone)) {
	    res = ERROR_GONE;
	} else if (errorCode.equals(item_not_found)) {
	    res = ERROR_ITEM_NOT_FOUND;
	} else if (errorCode.equals(jid_malformed)) {
	    res = ERROR_JID_MALFORMED;
	} else if (errorCode.equals(no_acceptable)) {
	    res = ERROR_NO_ACCEPTABLE;
	} else if (errorCode.equals(not_allowed)) {
	    res = ERROR_NOT_ALLOWED;
	} else if (errorCode.equals(not_authorized)) {
	    res = ERROR_NOT_AUTHORIZED;
	} else if (errorCode.equals(payment_required)) {
	    res = ERROR_PAYMENT_REQUIRED;
	} else if (errorCode.equals(recipient_unavailable)) {
	    res = ERROR_RECIPIENT_UNAVAILABLE;
	} else if (errorCode.equals(redirect)) {
	    res = ERROR_REDIRECT;
	} else if (errorCode.equals(registration_required)) {
	    res = ERROR_REGISTRATION_REQUIRED;
	} else if (errorCode.equals(remote_server_error)) {
	    res = ERROR_REMOTE_SERVER_ERROR;
	} else if (errorCode.equals(remote_server_not_found)) {
	    res = ERROR_REMOTE_SERVER_NOT_FOUND;
	} else if (errorCode.equals(remote_server_timeout)) {
	    res = ERROR_REMOTE_SERVER_TIMEOUT;
	} else if (errorCode.equals(resource_constraint)) {
	    res = ERROR_RESOURCE_CONSTRAINT;
	} else if (errorCode.equals(service_unavailable)) {
	    res = ERROR_SERVICE_UNAVAILABLE;
	} else if (errorCode.equals(subscription_required)) {
	    res = ERROR_SUBSCRIPTION_REQUIRED;
	} else if (errorCode.equals(undefined_String)) {
	    res = ERROR_UNDEFINED_STRING;
	} else if (errorCode.equals(unexpected_request)) {
	    res = ERROR_UNEXPECTED_REQUEST;
	} else if (errorCode.equals(request_timeout)) {
	    res = ERROR_REQUEST_TIMEOUT;
	} else {
	    return ERROR_UNKNOWN;
	}
	return res;
    }

    /**
     * Get the error type from a presence packet.
     * @param presence the presence type
     * @return an int representing the presence type
     */
    public static String getErrorMsg(Context c, int errorCode) {
	String res = "";
	switch (errorCode) {
	    case NoError:
		res = "";
		break;
	    case ERROR_INTERNA_SERVER_ERROR:
		res = c.getString(R.string.interna_server_error);
		break;
	    case ERROR_FORBIDDEN:
		res = c.getString(R.string.forbidden);
		break;
	    case ERROR_BAD_REQUEST:
		res = c.getString(R.string.bad_request);
		break;
	    case ERROR_CONFLICT:
		res = c.getString(R.string.conflict);
		break;
	    case ERROR_FEATURE_NOT_IMPLEMENTED:
		res = c.getString(R.string.feature_not_implemented);
		break;
	    case ERROR_GONE:
		res = c.getString(R.string.gone);
		break;
	    case ERROR_ITEM_NOT_FOUND:
		res = c.getString(R.string.item_not_found);
		break;
	    case ERROR_JID_MALFORMED:
		res = c.getString(R.string.jid_malformed);
		break;
	    case ERROR_NO_ACCEPTABLE:
		res = c.getString(R.string.no_acceptable);
		break;
	    case ERROR_NOT_ALLOWED:
		res = c.getString(R.string.not_allowed);
		break;
	    case ERROR_NOT_AUTHORIZED:
		res = c.getString(R.string.not_authorized);
		break;
	    case ERROR_PAYMENT_REQUIRED:
		res = c.getString(R.string.payment_required);
		break;
	    case ERROR_RECIPIENT_UNAVAILABLE:
		res = c.getString(R.string.recipient_unavailable);
		break;
	    case ERROR_REDIRECT:
		res = c.getString(R.string.redirect);
		break;
	    case ERROR_REGISTRATION_REQUIRED:
		res = c.getString(R.string.registration_required);
		break;
	    case ERROR_REMOTE_SERVER_ERROR:
		res = c.getString(R.string.remote_server_error);
		break;
	    case ERROR_REMOTE_SERVER_NOT_FOUND:
		res = c.getString(R.string.remote_server_not_found);
		break;
	    case ERROR_REMOTE_SERVER_TIMEOUT:
		res = c.getString(R.string.remote_server_timeout);
	    case ERROR_RESOURCE_CONSTRAINT:
		res = c.getString(R.string.resource_constraint);
		break;
	    case ERROR_SERVICE_UNAVAILABLE:
		res = c.getString(R.string.service_unavailable);
		break;
	    case ERROR_SUBSCRIPTION_REQUIRED:
		res = c.getString(R.string.subscription_required);
		break;
	    case ERROR_UNDEFINED_STRING:
		res = c.getString(R.string.undefined_String);
		break;
	    case ERROR_UNEXPECTED_REQUEST:
		res = c.getString(R.string.unexpected_request);
		break;
	    case ERROR_REQUEST_TIMEOUT:
		res = c.getString(R.string.request_timeout);
		break;
	    case ERROR_UNKNOWN:
		res = "unknown error";
		break;
	    default:
		res = "";
	}
	return res;
    }

    public static String getErrorMsg(Context c, String errorCode) {
	if (errorCode.equals(interna_server_error)) {
	    return c.getString(R.string.interna_server_error);
	} else if (errorCode.equals(forbidden)) {
	    return c.getString(R.string.forbidden);
	} else if (errorCode.equals(bad_request)) {
	    return c.getString(R.string.bad_request);
	} else if (errorCode.equals(conflict)) {
	    return c.getString(R.string.conflict);
	} else if (errorCode.equals(feature_not_implemented)) {
	    return c.getString(R.string.feature_not_implemented);
	} else if (errorCode.equals(gone)) {
	    return c.getString(R.string.gone);
	} else if (errorCode.equals(item_not_found)) {
	    return c.getString(R.string.item_not_found);
	} else if (errorCode.equals(jid_malformed)) {
	    return c.getString(R.string.jid_malformed);
	} else if (errorCode.equals(no_acceptable)) {
	    return c.getString(R.string.no_acceptable);
	} else if (errorCode.equals(not_allowed)) {
	    return c.getString(R.string.not_allowed);
	} else if (errorCode.equals(not_authorized)) {
	    return c.getString(R.string.not_authorized);
	} else if (errorCode.equals(payment_required)) {
	    return c.getString(R.string.payment_required);
	} else if (errorCode.equals(recipient_unavailable)) {
	    return c.getString(R.string.recipient_unavailable);
	} else if (errorCode.equals(redirect)) {
	    return c.getString(R.string.redirect);
	} else if (errorCode.equals(registration_required)) {
	    return c.getString(R.string.registration_required);
	} else if (errorCode.equals(remote_server_error)) {
	    return c.getString(R.string.remote_server_error);
	} else if (errorCode.equals(remote_server_not_found)) {
	    return c.getString(R.string.remote_server_not_found);
	} else if (errorCode.equals(remote_server_timeout)) {
	    return c.getString(R.string.remote_server_timeout);
	} else if (errorCode.equals(resource_constraint)) {
	    return c.getString(R.string.resource_constraint);
	} else if (errorCode.equals(service_unavailable)) {
	    return c.getString(R.string.service_unavailable);
	} else if (errorCode.equals(subscription_required)) {
	    return c.getString(R.string.subscription_required);
	} else if (errorCode.equals(undefined_String)) {
	    return c.getString(R.string.undefined_String);
	} else if (errorCode.equals(unexpected_request)) {
	    return c.getString(R.string.unexpected_request);
	} else if (errorCode.equals(request_timeout)) {
	    return c.getString(R.string.request_timeout);
	} else {
	    return "error";
	}
    }

}
